package io.renren.modules.communityService.vo;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;

import java.awt.*;

/**
 * @author chenweilong
 * @email 1433471850@qq.com
 * @date 2021-03-21 14:16
 */

@Data
@Accessors(chain = true)
public class MiniAppElectronicAccessCertificateGetVO {

    /**
     * 二维码
     */
    @ApiModelProperty(required=false,value="用户id")
    public String qrCode;
    /**
     * 用户id
     */
    @ApiModelProperty(required=false,value="用户id")
    private Integer userId;
    /**
     * 用户昵称
     */
    @ApiModelProperty(required=false,value="用户昵称")
    private String userNickname;
    /**
     * 码的颜色
     */
    @ApiModelProperty(required=false,value="码的颜色")
    private int color;

    public String getQrCode() {
        String promoCode = String.format("https://www.baidu.com?id=%s",userId);
        if (StrUtil.isNotBlank(promoCode)) {

            QrConfig config = new QrConfig(300, 300);
            // 设置边距，既二维码和背景之间的边距
            config.setMargin(3);
            config.setForeColor(color);


            byte[] bytes = QrCodeUtil.generatePng(promoCode,config);
            return "data:image/png;base64," + Base64.encode(bytes);
        }
        return null;
    }
}
